#ifndef __WEB_HOST_H__
#define __WEB_HOST_H__

#include "base/object.h"
#include "container/inputhelper.h"

#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <vector>

struct OpaqueWKView;
typedef const struct OpaqueWKView* WKViewRef;

namespace MiniUI
{

class Timer;
class WebHost;
typedef void (Delegater::*OnInvalidateFunc)(WebHost* pWebHost, Type::MiniRect* rcUpdate);

class WebPage;
class WebHost : public Object
{
public:
    WebHost(WebPage* pWebPage);
    ~WebHost();
public:
    //WebOperation
    bool LoadURL(const char* szURL);
public:
    //Input Message & MouseActivate & All Messages
    HRESULT SendMessage(UINT Msg, WPARAM wParam, LPARAM lParam, bool& handled);
    HRESULT SendMessage(UINT Msg, WPARAM wParam, LPARAM lParam);
    //Paint
    byte* DoPaint(const Type::MiniRect& rcRender, Type::MiniSize& bitsBound);
    //ShowWindow
    bool DoVisible(bool bVisible);
    //Close & Destroy
    bool DoFinalize();
    //Focus & WindowAcitvate
    bool DoFocus(bool bFocus);
    bool DoWindowActivate(bool bActivate);
    //Size & Pos
    bool DoSize(const Type::MiniSize& size);
    bool DoMove(const Type::MiniPoint& pt);
    //Mouse
    bool DoMouseDown(const Type::MiniPoint& pt, MouseButton button, uint keyState, bool& handled);
    bool DoDblClk(const Type::MiniPoint& pt, MouseButton button, uint keyState);
    bool DoMouseUp(const Type::MiniPoint& pt, MouseButton button, uint keyState);
    bool DoMouseMove(const Type::MiniPoint& pt, uint keyState);
    bool DoMouseLeave();
    bool DoMouseWheel(const Type::MiniPoint& pt, uint keyState, int16 zDelta);
    //Timer
    void DoTimer(const Object* sender);
private:
    void InitWebKit();
    //static WebView UI Clent API
    static void SetTimer(WKViewRef webview, UINT nID, UINT nInterval);
    static void KillTimer(WKViewRef webview, UINT nID);
    static void GetCursorPos(WKViewRef webview, POINT& pt);
    static void ClientToScreen(WKViewRef webview, POINT& pt);
    static void ScreenToClient(WKViewRef webview, POINT& pt);
    static void GetClientRect(WKViewRef webview, RECT& rcWnd);
    static void SetCursor(WKViewRef webview, HCURSOR newCursor);
    static bool IsWindowActive(WKViewRef webview);
    static void*GetHost(WKViewRef webview);
    //static WebKit Event Callback
    static void OnPaintEvent(WKViewRef webview, void* lpBits, int boundWidth, int boundHeight);
    static void OnInvalidateEvent(WKViewRef webview, RECT* rect);
    static void OnProcessCrashEvent(WKViewRef webview);
    static void OnRelaunchProcessEvent(WKViewRef webview);
public:
    CallEvent<OnInvalidateFunc> OnInvalidate;
private:
    void* m_webView;    //in webkit
    WebPage* m_pWebPage;
    lpvoid m_pBytes;
    Type::MiniSize m_bound;
    std::vector<Timer*> m_timerList;
    bool m_isWindowActive;
};

}

#endif
